package cn.ydw.community.controller;

import cn.ydw.community.entity.PageDTO;
import cn.ydw.community.entity.DiscussPost;
import cn.ydw.community.entity.User;
import cn.ydw.community.service.DiscussPostService;
import cn.ydw.community.service.LikeService;
import cn.ydw.community.service.UserService;
import cn.ydw.community.util.HostHolder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class IndexController {

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private UserService userService;

    @Autowired
    LikeService likeService;

    @Autowired
    HostHolder hostHolder;

    @GetMapping("/index")
    public String getDiscussPost(@RequestParam(value = "orderMode", defaultValue = "0") Integer orderMode, Model model, Page<DiscussPost> page) {
        User currentUser = hostHolder.getUser();
        List<DiscussPost> discussPosts;
        model.addAttribute("user", currentUser);
        model.addAttribute("orderMode", orderMode);
        // 设置分页属性
        page.setSize(8);
        if (orderMode == 0) {
            page.setDesc("type", "create_time");
            discussPosts = discussPostService.getDiscussPost(page);

        } else if (orderMode == 1) {
            page.setDesc("score", "create_time");
            discussPosts = discussPostService.getDiscussPost(page);
        } else {
            page.setDesc("create_time");
            if (currentUser != null && (currentUser.getType() != 1 && currentUser.getType() != 2)) {
                discussPosts = discussPostService.findDiscussPostByType(page, currentUser.getType());
            } else {
                return "redirect:/index";
            }
        }
//        discussPosts = discussPostService.getDiscussPost(page);
        // 将文章信息 以及 用户信息 存到Map中，然后循环遍历discussPosts中的文章信息
        List<Map<String, Object>> lists = new ArrayList<>();
        if (discussPosts != null) {
            for (DiscussPost discussPost : discussPosts) {
                Map<String, Object> map = new HashMap<>();
                map.put("post", discussPost);
                User user = userService.selectUserById(Integer.valueOf(discussPost.getUserId()));
                map.put("user", user);
                //点赞数
                long likeCount = likeService.findEntityLikeCount(1, discussPost.getId());
                map.put("likeCount", likeCount);
                lists.add(map);
            }
        }
        // 封装page对象导PageDTO中，后面可以优化
        PageDTO pageDTO = new PageDTO(page);
        pageDTO = pageDTO.pageDTO();
        pageDTO.setPath("/index?orderMode=" + orderMode);
        model.addAttribute("pagedto", pageDTO);
        log.info("封装Page：" + pageDTO);
        model.addAttribute("page", page);
        model.addAttribute("discussPosts", lists);
        return "index";
    }

    @GetMapping("/error")
    public String errorPage() {
        return "error/500";
    }

}
